/*
 *
 *  * | Licensed 未经许可不能去掉「OPENIITA」相关版权
 *  * +----------------------------------------------------------------------
 *  * | Author: xw2sy@163.com
 *  * +----------------------------------------------------------------------
 *
 *  Copyright [2024] [OPENIITA]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * /
 */

package cc.iotkit.manager.service.impl;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.manager.IDeviceInfoPartsData;
import cc.iotkit.data.manager.IDevicePartsData;
import cc.iotkit.manager.dto.bo.device.DeviceInfoPartsBo;
import cc.iotkit.manager.dto.bo.device.DevicePartsBo;
import cc.iotkit.manager.dto.vo.deviceinfo.DeviceInfoPartsVo;
import cc.iotkit.manager.dto.vo.deviceparts.DevicePartsVo;
import cc.iotkit.manager.service.IDeviceInfoPartsService;
import cc.iotkit.manager.service.IDevicePartsService;
import cc.iotkit.model.device.DeviceInfoParts;
import cc.iotkit.model.device.DeviceParts;
import cn.hutool.core.collection.CollectionUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 租户Service业务层处理
 *
 * @author Michelle.Chung
 */
@RequiredArgsConstructor
@Service
public class DeviceInfoPartsServiceImpl implements IDeviceInfoPartsService {

    private final IDeviceInfoPartsData data;

    public DeviceInfoPartsVo queryById(Long id) {
        return MapstructUtils.convert(data.findById(id),DeviceInfoPartsVo.class);
    }

    /**
     * 新增保存角色信息
     *
     * @param bo 角色信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(DeviceInfoPartsBo bo) {
        data.save(bo.to(DeviceInfoParts.class));
        return true;
    }

    @Transactional
    public List<DeviceInfoPartsVo> saveAll(List<DeviceInfoPartsBo> datas){
        //判断设备配件是否重复
        int size= datas.stream().map(m->m.getDevicePartsId()).distinct().collect(Collectors.toList()).size();
        if(size<datas.size()){
            throw new BizException("配件不可重新");
        }
        String deviceId=datas.get(0).getDeviceId();
        List<DeviceInfoParts> newList=  MapstructUtils.convert(datas,DeviceInfoParts.class);
        //获取原始数据
        DeviceInfoParts cond=new DeviceInfoParts();
        cond.setDeviceId(deviceId);
        List<DeviceInfoParts> oldList=data.findAllByCondition(cond);

        //去除差异数据
        List<Long> oldIds=oldList.stream().map(item->item.getId()).collect(Collectors.toList());
        List<Long> newIds=newList.stream().filter(m->m.getId()!=null).map(item->item.getId()).collect(Collectors.toList());
        Collection<Long> delIds = CollectionUtil.subtract(oldIds, newIds).stream().collect(Collectors.toList());
        data.deleteByIds(delIds);

        //保存数据
        List<DeviceInfoPartsVo> results=  MapstructUtils.copyListToList(data.saveAll(newList),DeviceInfoPartsVo.class);
        return results;
    }

    @Override
    public boolean deleteById(Long id) {
        data.deleteById(id);
        return true;
    }

    @Transactional
    public boolean deleteByIds(Collection<Long> ids) {
        data.deleteByIds(ids);
        return true;
    }



    @Override
    public Paging<DeviceInfoPartsVo> queryPageList(PageRequest<DeviceInfoPartsBo> request) {
        Paging<DeviceInfoParts> pages=data.findAllByConditions(request.to(DeviceInfoParts.class));
        return pages.to(DeviceInfoPartsVo.class);
    }

    @Override
    public List<DeviceInfoPartsVo> queryList(DeviceInfoPartsBo bo) {
        List<DeviceInfoParts> list=data.findAllByCondition(bo.to(DeviceInfoParts.class));
        return MapstructUtils.copyListToList(list,DeviceInfoPartsVo.class);
    }



}
